package com.itheima.demo1;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Demo1 {
    /*
    定义一个字符输出缓冲流BufferedWriter对象，
    将学员信息写入到Task1.txt文件中，每个学员信息占一行
    ，每个字段之间用","符号隔开。例如：

- 要求：写入的数据必须按照年龄升序排序，当年龄相同时再按照分数降序排序
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        List<Student> stuList = new ArrayList<Student>();
        stuList.add(new Student("孤立拿扎", "女", 19, 88));
        stuList.add(new Student("地理那把", "女", 19, 78));
        stuList.add(new Student("周董", "男", 30, 100));
        stuList.add(new Student("韬奋南海", "男", 65, 78));

        Collections.sort(stuList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int num = o1.getAge() - o2.getAge();
                if (num == 0) {
                    num = (int) (o2.getScore() - o1.getScore());
                }
                return num;
            }
        });

        BufferedWriter bw = new BufferedWriter(new FileWriter("Task1.txt"));


        for (Student student : stuList) {
            bw.newLine();
            bw.write(student.toString());
           // System.out.println(student.toString());
        }
        //创建字符输出缓冲流
       bw.close();



        //------------序列化加入--------
      /*  ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("Task1.txt")));
        for (Student student : stuList) {
            oos.writeObject(System.lineSeparator());
            oos.writeObject(student.toString());
        }
      oos.close();


ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream("Task1.txt")));
        for (Student student : stuList) {
            System.out.println(student.toString());
        }

        *//*Object readObject = ois.readObject();
        System.out.println(readObject);
        Object readObject2 = ois.readObject();
        System.out.println(readObject2);
        Object readObject3 = ois.readObject();
        System.out.println(readObject3);
        Object readObject4 = ois.readObject();
        System.out.println(readObject4);*//*

        ois.close();
*/
    }

}
